package cn.yogehaoren;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author WangNing
 * @since 1.0
 */
@Configuration
@ComponentScan(basePackages = "cn.yogehaoren")
public class RabbitConfig {

    @Bean
    public ConnectionFactory connectionFactory(){
        com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = new com.rabbitmq.client.ConnectionFactory();
        rabbitConnectionFactory.setHost("k8s-node2");
        rabbitConnectionFactory.setPort(5672);
        rabbitConnectionFactory.setVirtualHost("/");
        return new CachingConnectionFactory(rabbitConnectionFactory);

    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }



    @Bean
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory());
    }

    @Bean
    public TopicExchange exchange1(){
        return new TopicExchange("topic001", false, true);
    }

    @Bean
    public Queue queue1(){
        return new Queue("topic_001_queue", false);
    }

    @Bean
    public Binding binding1(){
        return  BindingBuilder.bind(queue1()).to(exchange1()).with("#");
    }



}
